package com.sxm.yiwei.pinche;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.amap.api.services.help.Tip;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkRouteResult;
import com.bigkoo.pickerview.TimePickerView;
import com.sxm.yiwei.R;
import com.sxm.yiwei.activitis.MineMember;
import com.sxm.yiwei.base.BaseActivity;
import com.sxm.yiwei.beans.pinche.HostOrder;
import com.sxm.yiwei.beans.pinche.PassengerOrder;
import com.sxm.yiwei.constant.Common;
import com.sxm.yiwei.constant.Constant;
import com.sxm.yiwei.tools.DialogMain;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.Calculator;
import com.sxm.yiwei.utils.DateUtil;
import com.sxm.yiwei.utils.HandleDataUtil;
import com.sxm.yiwei.utils.SPUtil;
import com.sxm.yiwei.utils.WidgetUtil;
import com.sxm.yiwei.views.AmountView;
import com.sxm.yiwei.views.CenterDialog;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class ReleaseActivity extends BaseActivity implements View.OnClickListener, CenterDialog.OnCenterItemClickListener, RouteSearch.OnRouteSearchListener {

    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_long_way;
    private TextView tv_same_city;
    private TextView tv_travel;
    private TextView tv_long_hurry;
    private TextView tv_start_pos;
    private TextView tv_end_pos;
    private TextView tv_start_time;
    private TextView tv_break_appointment_money;
    private TextView tv_transfer_fee;
    private CheckBox checkBox;
    private TextView tv_rule;
    private Button btn_commit;
    private LinearLayout ll_add;
    private TextView tv_price;
    private TextView tv_pinche_count_rule;
    private TextView tv_total_price;
    private EditText et_baojia;
    private ArrayList<TextView> viewList;
    private int identity;
    private LinearLayout ll_host_long;
    private LinearLayout ll_passenger_long_hurry;
    private LinearLayout ll_passenger_long;
    private LinearLayout ll_host_long_hurry;
    private int type_flag=Constant.LONG;
    private AmountView amount_view_passenger_long;
    private TextView tv_price_passenger_long;
    private TextView tv_fee_rule_passenger_long;
    private AmountView amount_view_passenger_long_hurry;
    private TextView tv_container_passenger_long_hurry;
    private TextView tv_transfer_fee_passenger_long_hurry;
    private TextView tv_fee_rule_passenger_long_hurry;
    private EditText et_baojia_passenger_long_hurry;
    private AmountView amount_view_host_long;
    private TextView tv_pinche_count_rule_host_long;
    private TextView tv_total_price_host_long;
    private TextView tv_break_appointment_money_host_long;
    private TextView tv_break_appointment_money_host_long_hurry;
    private TextView tv_transfer_fee_host_long_hurry;
    private TextView tv_fee_rule_host_long_hurry;
    private AmountView amount_view_host_long_hurry;
    private TextView tv_container_host_long_hurry;
    private CenterDialog centerDialog;
    private int failure_code;
    private ProgressDialog progressDialog;
    //乘客长途
    private double total_amount_passenger_long=0.0;
    private double single_amount_passenger_long=0.0;
    private double distance_passenger_long=0.0;
    private int seats_passenger_long=1;
    //车主长途
    private double total_amount_host_long;
    private double single_amount_host_long;
    private double loss_amount_host_long;
    private int seats_host_long=1;
    private double distance_host_long=0.0;
    private EditText etAmount_host_long;
    private EditText etAmount_host_long_hurry;
    private EditText etAmount_passenger_long;
    private EditText etAmount_passenger_long_hurry;
    private EditText et_price_host_long;
    private double single_money;
    private RouteSearch routeSearch;
    private Tip startTip;//接受选择地点tip
    private Tip endTip;//接受结束地点tip
    private String start="";
    private String end="";


    @Override
    public void setContentView() {
        setContentView(R.layout.activity_release);
    }

    @Override
    public void initView() {
        rl_back = (RelativeLayout)findViewById(R.id.rl_back);
        tv_title = (TextView)findViewById(R.id.tv_title);
        tv_long_way = (TextView)findViewById(R.id.tv_long_way);
        tv_same_city = (TextView)findViewById(R.id.tv_same_city);
        tv_travel = (TextView)findViewById(R.id.tv_travel);
        tv_long_hurry = (TextView)findViewById(R.id.tv_long_hurry);
        tv_start_pos = (TextView)findViewById(R.id.tv_start_pos);
        tv_end_pos = (TextView)findViewById(R.id.tv_end_pos);
        tv_start_time = (TextView)findViewById(R.id.tv_start_time);
        checkBox = (CheckBox)findViewById(R.id.checkbox);
        tv_rule = (TextView)findViewById(R.id.tv_rule);
        btn_commit = (Button)findViewById(R.id.btn_commit);
        ll_host_long = (LinearLayout)findViewById(R.id.ll_host_long);
        ll_passenger_long_hurry = (LinearLayout)findViewById(R.id.ll_passenger_long_hurry);
        ll_passenger_long = (LinearLayout)findViewById(R.id.ll_passenger_long);
        ll_host_long_hurry = (LinearLayout)findViewById(R.id.ll_host_long_hurry);


        //车主长途急件
        tv_break_appointment_money_host_long_hurry = (TextView)findViewById(R.id.tv_break_appointment_money_host_long_hurry);
        tv_transfer_fee_host_long_hurry = (TextView)findViewById(R.id.tv_transfer_fee_host_long_hurry);
        tv_fee_rule_host_long_hurry = (TextView)findViewById(R.id.tv_fee_rule_host_long_hurry);
        amount_view_host_long_hurry = (AmountView)findViewById(R.id.amount_view_host_long_hurry);
        tv_container_host_long_hurry = (TextView)findViewById(R.id.tv_container_host_long_hurry);
        etAmount_host_long_hurry = (EditText) amount_view_host_long_hurry.findViewById(R.id.etAmount);

        //车主长途急件

        //车主长途
        amount_view_host_long=(AmountView)findViewById(R.id.amount_view_host_long);
        etAmount_host_long = (EditText) amount_view_host_long.findViewById(R.id.etAmount);

        et_price_host_long = (EditText)findViewById(R.id.et_price_host_long);
        tv_pinche_count_rule_host_long = (TextView)findViewById(R.id.tv_pinche_count_rule_host_long);
        tv_total_price_host_long = (TextView)findViewById(R.id.tv_total_price_host_long);
        tv_break_appointment_money_host_long=(TextView)findViewById(R.id.tv_break_appointment_money_host_long);
        //车主长途

        //乘客长途
        amount_view_passenger_long=(AmountView)findViewById(R.id.amount_view_passenger_long);
        etAmount_passenger_long = (EditText) amount_view_passenger_long.findViewById(R.id.etAmount);

        tv_price_passenger_long=(TextView)findViewById(R.id.tv_price_passenger_long);
        tv_fee_rule_passenger_long = (TextView)findViewById(R.id.tv_fee_rule_passenger_long);
        //乘客长途

        //乘客长途急件
        amount_view_passenger_long_hurry=(AmountView)findViewById(R.id.amount_view_passenger_long_hurry);
        etAmount_passenger_long_hurry = (EditText) amount_view_passenger_long_hurry.findViewById(R.id.etAmount);

        tv_container_passenger_long_hurry=(TextView)findViewById(R.id.tv_container_passenger_long_hurry);
        tv_transfer_fee_passenger_long_hurry=(TextView)findViewById(R.id.tv_transfer_fee_passenger_long_hurry);
        tv_fee_rule_passenger_long_hurry=(TextView)findViewById(R.id.tv_fee_rule_passenger_long_hurry);
        et_baojia_passenger_long_hurry = (EditText)findViewById(R.id.et_baojia_passenger_long_hurry);

        //乘客长途急件

        amount_view_passenger_long.setGoods_storage(1000);
        amount_view_host_long.setGoods_storage(1000);
        amount_view_host_long_hurry.setGoods_storage(1000);
        amount_view_passenger_long_hurry.setGoods_storage(1000);

        btn_commit.setEnabled(false);
        routeSearch = new RouteSearch(this);
        routeSearch.setRouteSearchListener(this);


    }

    @Override
    public void initListener() {
        rl_back.setOnClickListener(this);
        tv_long_way.setOnClickListener(this);
        tv_same_city.setOnClickListener(this);
        tv_travel.setOnClickListener(this);
        tv_long_hurry.setOnClickListener(this);
        tv_start_time.setOnClickListener(this);
        tv_fee_rule_host_long_hurry.setOnClickListener(this);
        tv_fee_rule_passenger_long.setOnClickListener(this);
        tv_fee_rule_passenger_long_hurry.setOnClickListener(this);
        tv_rule.setOnClickListener(this);
        tv_container_host_long_hurry.setOnClickListener(this);
        tv_container_passenger_long_hurry.setOnClickListener(this);
        btn_commit.setOnClickListener(this);
        tv_start_pos.setOnClickListener(this);
        tv_end_pos.setOnClickListener(this);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!b){
                    btn_commit.setBackgroundDrawable(getResources().getDrawable(R.drawable.commit_order_btn_disable));
                    btn_commit.setEnabled(false);
                }else {
                    btn_commit.setBackgroundDrawable(getResources().getDrawable(R.drawable.commit_order_btn_enable));
                    btn_commit.setEnabled(true);
                }
            }
        });
        amount_view_passenger_long_hurry.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, int amount) {
            }
        });
        amount_view_passenger_long.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, int amount) {

                if (distance_passenger_long!=0){
                    total_amount_passenger_long = new Calculator().getTotalAmountWithPassenger(type_flag+"",distance_passenger_long)*amount;
                }
                tv_price_passenger_long.setText(HandleDataUtil.saveToPoint2(total_amount_passenger_long) +"元");
            }
        });
        amount_view_host_long_hurry.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, int amount) {
            }
        });
        amount_view_host_long.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, int amount) {
                if (distance_host_long!=0){
                    total_amount_host_long=new Calculator().getTotalAmountWithHost(type_flag+"",Constant.PRIVATE_CAR+"",distance_host_long)*amount;
                    loss_amount_host_long=total_amount_host_long*0.1;
                }

                tv_total_price_host_long.setText(HandleDataUtil.saveToPoint2(total_amount_host_long)+"元");
                tv_break_appointment_money_host_long.setText("最高"+HandleDataUtil.saveToPoint2(loss_amount_host_long)+"元");
            }
        });
        tv_start_pos.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                /*if (startTip!=null&&endTip!=null){
                    doRoutePlan(startTip.getAdcode(),endTip.getAdcode());
                }*/
                start=charSequence.toString();
                if (identity==Constant.PASSENGER&&type_flag!=Constant.LONG_HURRY){
                    distance_passenger_long=getDistance(start,end);
                    seats_passenger_long=Integer.parseInt(etAmount_passenger_long.getText().toString());
                    if (distance_passenger_long!=0){
                        single_amount_passenger_long=new Calculator().getTotalAmountWithPassenger(type_flag+"",distance_passenger_long);
                    }else {
                        single_amount_passenger_long=0;

                    }
                    total_amount_passenger_long = single_amount_passenger_long*seats_passenger_long;
                    tv_price_passenger_long.setText(HandleDataUtil.saveToPoint2(total_amount_passenger_long) +"元");
                }else if (identity==Constant.HOST&&type_flag!=Constant.LONG_HURRY){
                    distance_host_long=getDistance(start,end);
                    seats_host_long=Integer.parseInt(etAmount_host_long.getText().toString());
                    if (distance_host_long!=0){
                        single_amount_host_long=new Calculator().getTotalAmountWithHost(type_flag+"",Constant.PRIVATE_CAR+"",distance_host_long);
                    }else {
                        single_amount_host_long=0;
                    }
                    total_amount_host_long=single_amount_host_long*seats_host_long;
                    loss_amount_host_long=total_amount_host_long*0.1;
                    et_price_host_long.setText(HandleDataUtil.saveToPoint2(single_amount_host_long)+"");
                    tv_total_price_host_long.setText(HandleDataUtil.saveToPoint2(total_amount_host_long)+"元");
                    tv_break_appointment_money_host_long.setText("最高"+HandleDataUtil.saveToPoint2(loss_amount_host_long)+"元");
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        tv_end_pos.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
               /* if (startTip!=null&&endTip!=null){
                    doRoutePlan(startTip.getAdcode(),endTip.getAdcode());
                }*/
                end=charSequence.toString();
                if (identity==Constant.PASSENGER&&type_flag!=Constant.LONG_HURRY){
                    distance_passenger_long=getDistance(start,end);
                    seats_passenger_long=Integer.parseInt(etAmount_passenger_long.getText().toString());
                    if (distance_passenger_long!=0){
                        single_amount_passenger_long=new Calculator().getTotalAmountWithPassenger(type_flag+"",distance_passenger_long);

                    }else {
                        single_amount_passenger_long=0;

                    }
                    total_amount_passenger_long = single_amount_passenger_long*seats_passenger_long;
                    tv_price_passenger_long.setText(HandleDataUtil.saveToPoint2(total_amount_passenger_long) +"元");
                }else if (identity==Constant.HOST&&type_flag!=Constant.LONG_HURRY){
                    distance_host_long=getDistance(start,end);
                    seats_host_long=Integer.parseInt(etAmount_host_long.getText().toString());
                    if (distance_host_long!=0){
                        single_amount_host_long=new Calculator().getTotalAmountWithHost(type_flag+"",Constant.PRIVATE_CAR+"",distance_host_long);

                    }else {
                        single_amount_host_long=0;

                    }
                    total_amount_host_long=single_amount_host_long*seats_host_long;
                    loss_amount_host_long=total_amount_host_long*0.1;
                    et_price_host_long.setText(HandleDataUtil.saveToPoint2(single_amount_host_long)+"");
                    tv_total_price_host_long.setText(HandleDataUtil.saveToPoint2(total_amount_host_long)+"元");
                    tv_break_appointment_money_host_long.setText("最高"+HandleDataUtil.saveToPoint2(loss_amount_host_long)+"元");
                }

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        et_price_host_long.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(editable.toString())){
                    single_money=0.00;
                }else {
                    single_money=Double.parseDouble(editable.toString());
                }
                if (single_money>single_amount_host_long*1.5||single_money<single_amount_host_long*0.5){
                    showToast("您输入的单价超过浮动范围");
                }else {
                    total_amount_host_long=single_money*Integer.parseInt(etAmount_host_long.getText().toString());
                    loss_amount_host_long=total_amount_host_long*0.1;
                    tv_total_price_host_long.setText(HandleDataUtil.saveToPoint2(total_amount_host_long)+"元");
                    tv_break_appointment_money_host_long.setText("最高"+HandleDataUtil.saveToPoint2(loss_amount_host_long)+"元");
                }

            }
        });

    }
    public double getDistance(String start_place,String end_place ){
        if (!TextUtils.isEmpty(start_place)&&!TextUtils.isEmpty(end_place)){
            if (start_place.equals(end_place)){
                return 0;
            }else {
                return 100;//计算两点间距离；模拟100km
            }
        }else {
            return 0;
        }

    }


    @Override
    public void initData() {
        identity = getIntent().getIntExtra("identity",0);//1 乘客 2 车主
        switch (identity){
            case Constant.PASSENGER:
                tv_title.setText("我是乘客-发布");
                selectReleaseItem(identity,type_flag);
                break;
            case Constant.HOST:
                tv_title.setText("我是车主-发布");
                selectReleaseItem(identity,type_flag);
                break;
        }

        viewList=new ArrayList<>();
        viewList.add(tv_long_way);
        viewList.add(tv_same_city);
        viewList.add(tv_travel);
        viewList.add(tv_long_hurry);
    }
    /*
    * identity 用户身份 1 乘客 2 车主
    * type 运送类型 0 长途 1 同城 2 旅游 3 长途急件
    * */
    private void selectReleaseItem(int identity,int type) {
        tv_start_pos.setText("");
        tv_end_pos.setText("");
        tv_start_time.setText("");
        if (identity==Constant.PASSENGER){
            if (type==Constant.LONG_HURRY){
                ll_passenger_long.setVisibility(View.GONE);
                ll_host_long.setVisibility(View.GONE);
                ll_passenger_long_hurry.setVisibility(View.VISIBLE);
                ll_host_long_hurry.setVisibility(View.GONE);
                tv_transfer_fee_passenger_long_hurry.setText("0.0元");
                etAmount_passenger_long_hurry.setText("1");

            }else {
                ll_passenger_long.setVisibility(View.VISIBLE);
                ll_host_long.setVisibility(View.GONE);
                ll_passenger_long_hurry.setVisibility(View.GONE);
                ll_host_long_hurry.setVisibility(View.GONE);
                tv_price_passenger_long.setText("0.0元");
                etAmount_passenger_long.setText("1");
            }
        }else if (identity==Constant.HOST){
            if (type==Constant.LONG_HURRY){
                ll_passenger_long.setVisibility(View.GONE);
                ll_host_long.setVisibility(View.GONE);
                ll_passenger_long_hurry.setVisibility(View.GONE);
                ll_host_long_hurry.setVisibility(View.VISIBLE);
                tv_break_appointment_money_host_long_hurry.setText("0.0元");
                tv_transfer_fee_host_long_hurry.setText("0.0元");
                etAmount_host_long_hurry.setText("1");

            }else {
                ll_passenger_long.setVisibility(View.GONE);
                ll_host_long.setVisibility(View.VISIBLE);
                ll_passenger_long_hurry.setVisibility(View.GONE);
                ll_host_long_hurry.setVisibility(View.GONE);

                et_price_host_long.setText("0.0");
                tv_total_price_host_long.setText("0.0元");
                tv_break_appointment_money_host_long.setText("最高0.0元");
                etAmount_host_long.setText("1");

            }
        }

    }
    private void doRoutePlan(String startID,String endID){
        Log.d("startID",startID);
        Log.d("endID",endID);
        RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo();
        fromAndTo.setStartPoiID(startID);
        fromAndTo.setDestinationPoiID(endID);
        RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo,RouteSearch.DrivingDefault , null, null, "");
        routeSearch.calculateDriveRouteAsyn(query);

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.tv_long_way:
                if (type_flag!=Constant.LONG){
                    changeType(Constant.LONG);
                }
                break;
            case R.id.tv_same_city:
                if (type_flag!=Constant.CITY){
                    changeType(Constant.CITY);
                }
                break;
            case R.id.tv_travel:
                if (type_flag!=Constant.TRAVEL){
                    changeType(Constant.TRAVEL);

                }
                break;
            case R.id.tv_long_hurry:
                if (type_flag!=Constant.LONG_HURRY){
                    changeType(Constant.LONG_HURRY);

                }
                break;
            case R.id.tv_start_time:
                //showDatePicker(tv_start_time);
                showIOSDate(tv_start_time, TimePickerView.Type.YEAR_MONTH_DAY_HOUR_MIN,"出发时间",2);
                break;
            case R.id.tv_start_pos:
                startActivityForResult(new Intent(this,SelectPositionActivity.class),Constant.REQUEST_CODE_START_PLACE);
                break;
            case R.id.tv_end_pos:
                startActivityForResult(new Intent(this,SelectPositionActivity.class),Constant.REQUEST_CODE_END_PLACE);

                break;

            case R.id.tv_fee_rule_host_long_hurry:
                Intent countIntent = new Intent(this, RuleActivity.class);
                countIntent.putExtra("type", Constant.PINCHE_COUNT_RULE);
                startActivity(countIntent);
                break;
            case R.id.tv_fee_rule_passenger_long:
                Intent countIntent1 = new Intent(this, RuleActivity.class);
                countIntent1.putExtra("type", Constant.PINCHE_COUNT_RULE);
                startActivity(countIntent1);
                break;
            case R.id.tv_fee_rule_passenger_long_hurry:
                Intent countIntent2 = new Intent(this, RuleActivity.class);
                countIntent2.putExtra("type", Constant.PINCHE_COUNT_RULE);
                startActivity(countIntent2);
                break;

            case R.id.tv_rule:
                Intent countIntent3 = new Intent(this, RuleActivity.class);
                countIntent3.putExtra("type", Constant.PINCHE_COUNT_RULE);
                startActivity(countIntent3);
                break;
            case R.id.btn_commit:
                String isFloat="0";
                //EditText etAmount=null;
                if (TextUtils.isEmpty(tv_start_pos.getText().toString())){
                    showToast(this,"请选择出发地点");
                    return;
                }
                if (TextUtils.isEmpty(tv_end_pos.getText().toString())){
                    showToast(this,"请选择目的地");
                    return;
                }
                if (TextUtils.isEmpty(tv_start_time.getText().toString())){
                    showToast(this,"请选择出发时间");
                    return;
                }
                if (identity==Constant.PASSENGER&&type_flag==Constant.LONG_HURRY){
                    if (TextUtils.isEmpty(et_baojia_passenger_long_hurry.getText().toString())){
                        showToast(this,"请填写保价金额");
                        return;
                    }
                }
                if (TextUtils.isEmpty(etAmount_host_long.getText())||TextUtils.isEmpty(etAmount_host_long_hurry.getText())||TextUtils.isEmpty(etAmount_passenger_long.getText())||TextUtils.isEmpty(etAmount_passenger_long_hurry.getText())){
                    showToast("需求数量不能为空");
                    return;
                }

                //上传


                if (identity==Constant.PASSENGER){
                    PassengerOrder passengerOrder = new PassengerOrder();
                    passengerOrder.setStyle(type_flag);
                    passengerOrder.setStart_place(tv_start_pos.getText().toString().trim());
                    passengerOrder.setEnd_place(tv_end_pos.getText().toString().trim());
                    passengerOrder.setDepart_time(HandleDataUtil.withoutChinese(tv_start_time.getText().toString().trim()));
                    if (type_flag==Constant.LONG_HURRY){//乘客长途急件
                        passengerOrder.setVolume_num(Integer.parseInt(etAmount_passenger_long_hurry.getText().toString()));
                        passengerOrder.setVolume_type(getVolumeType());
                        passengerOrder.setTransfer_fee(Double.parseDouble(HandleDataUtil.withoutChinese(tv_transfer_fee_passenger_long_hurry.getText().toString().trim())));
                        passengerOrder.setBaojia(Double.parseDouble(et_baojia_passenger_long_hurry.getText().toString().trim()));
                        showToast("乘客长途急件发布");
                    }else {//乘客长途，同城，旅游
                        passengerOrder.setSeats_need(Integer.parseInt(etAmount_passenger_long.getText().toString()));
                        passengerOrder.setDistance(distance_passenger_long);
                        //单价
                        passengerOrder.setPrice(single_amount_passenger_long);
                        passengerOrder.setTotal_price(total_amount_passenger_long);

                        JSONObject object = new JSONObject();
                        object.put("pinStartPoint",passengerOrder.getStart_place());
                        object.put("pinEndPoint",passengerOrder.getEnd_place());
                        object.put("leaveOffTime",passengerOrder.getDepart_time());//2017-10-12 19:23
                        object.put("pinTotalDistance",distance_passenger_long+"");
                        object.put("pinServiceAccount",passengerOrder.getPrice()+"");//单价
                        object.put("pinServiceKinds",passengerOrder.getStyle()+"");//默认0；1：长途 ; 2:表示旅游；3表示同城
                        object.put("passerRequireSeats",passengerOrder.getSeats_need()+"");//座位需求个数
                        object.put("pinTotalAccount",passengerOrder.getTotal_price()+"");//总价
                        httpPost(Common.HOST+Common.PASSENGER_PUBLISH,object,Constant.PASSENGER_PUBLISH);
                    }

                    //intent.putExtra("order",passengerOrder);
                }else if (identity==Constant.HOST){
                    HostOrder hostOrder = new HostOrder();
                    hostOrder.setStyle(type_flag);
                    hostOrder.setStart_place(tv_start_pos.getText().toString().trim());
                    hostOrder.setEnd_place(tv_end_pos.getText().toString().trim());
                    hostOrder.setDepart_time(HandleDataUtil.withoutChinese(tv_start_time.getText().toString().trim()));
                    if (type_flag==Constant.LONG_HURRY){//车主长途急件
                        hostOrder.setVolume_num(Integer.parseInt(etAmount_host_long_hurry.getText().toString()));
                        hostOrder.setVolume_type(getVolumeType());
                        hostOrder.setBreak_money(Double.parseDouble(HandleDataUtil.withoutChinese(tv_break_appointment_money_host_long_hurry.getText().toString().trim())));
                        hostOrder.setTransfer_fee(Double.parseDouble(HandleDataUtil.withoutChinese(tv_transfer_fee_host_long_hurry.getText().toString().trim())));
                        showToast("车主 长途急件发布");

                    }else {//车主长途，同城，旅游
                        Log.d("车主发布提供座位数",etAmount_host_long.getText().toString());

                        hostOrder.setSeats(Integer.parseInt(etAmount_host_long.getText().toString()));
                        hostOrder.setDistance(distance_host_long);
                        // TODO: 2017/10/27  et_price_host_long要做判断
                        if(TextUtils.isEmpty(et_price_host_long.getText().toString().trim())){
                            showToast("单价不能为空");
                            return;
                        }
                        if (single_amount_host_long*0.5>Double.parseDouble(et_price_host_long.getText().toString().trim())||single_amount_host_long*1.5<Double.parseDouble(et_price_host_long.getText().toString().trim())){
                            showToast("您输入的单价超出浮动范围，请重新输入");
                            return;
                        }
                        if (single_amount_host_long!=Double.parseDouble(et_price_host_long.getText().toString().trim())){
                            isFloat="1";
                        }else {
                            isFloat="0";
                        }
                        hostOrder.setPrice(Double.parseDouble(HandleDataUtil.withoutChinese(et_price_host_long.getText().toString().trim())));
                        hostOrder.setTotal_price(Double.parseDouble(HandleDataUtil.withoutChinese(tv_total_price_host_long.getText().toString().trim())));
                        hostOrder.setBreak_money(Double.parseDouble(HandleDataUtil.withoutChinese(tv_break_appointment_money_host_long.getText().toString().trim())));
                        JSONObject object = new JSONObject();
                        object.put("pinStartPoint",hostOrder.getStart_place());
                        object.put("pinEndPoint",hostOrder.getEnd_place());
                        object.put("leaveOffTime",hostOrder.getDepart_time());//2017-10-12 19:23
                        object.put("pinTotalDistance",distance_host_long+"");
                        object.put("pinTotalAccount",hostOrder.getTotal_price()+"");//总金额
                        object.put("pinServiceAccount",hostOrder.getPrice()+"");//单价
                        object.put("pinLoseAccount",hostOrder.getBreak_money()+"");//爽约金
                        object.put("pinIsFloat",isFloat);
                        object.put("pinServiceKinds",hostOrder.getStyle()+"");//默认0；1：长途 ; 2:表示旅游；3表示同城
                        object.put("ownerHasSeats",hostOrder.getSeats()+"");//发布座位个数
                        httpPost(Common.HOST+Common.HOST_PUBLISH,object,Constant.HOST_PUBLISH);

                    }
                }

                break;
            case R.id.tv_container_host_long_hurry:
                WidgetUtil.showPop(this,tv_container_host_long_hurry);
                break;
            case R.id.tv_container_passenger_long_hurry:
                WidgetUtil.showPop(this,tv_container_passenger_long_hurry);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data!=null){

            if (requestCode== Constant.REQUEST_CODE_START_PLACE&&resultCode==Constant.RESULT_CODE_PLACE){
                startTip = data.getParcelableExtra("selected_tip");
                if (startTip.getName().contains("市")){
                    String s = startTip.getName().replace("市", "");
                    tv_start_pos.setText(s);
                }else {
                    tv_start_pos.setText(startTip.getName());
                }
            }else if (requestCode== Constant.REQUEST_CODE_END_PLACE&&resultCode==Constant.RESULT_CODE_PLACE){
                endTip = data.getParcelableExtra("selected_tip");
                if (endTip.getName().contains("市")){
                    String s = endTip.getName().replace("市", "");
                    tv_end_pos.setText(s);
                }else {
                    tv_end_pos.setText(endTip.getName());
                }
            }
        }
    }

    private void hostCommitOrder(String start_pos,String end_pos,String depart_time,String total_money
            ,String single_money,String type,String seats,String distance,String lose_money,String pinIsFloat){
        progressDialog = ProgressDialog.show(this,"加载","正在加载中...");
        JSONObject object = new JSONObject();
        object.put("pinStartPoint",start_pos);
        object.put("pinEndPoint",end_pos);
        object.put("leaveOffTime",depart_time);//2017-10-12 19:23
        object.put("pinTotalDistance",distance);
        object.put("pinTotalAccount",total_money);//总金额
        object.put("pinServiceAccount",single_money);//单价
        object.put("pinLoseAccount",lose_money);//爽约金
        object.put("pinIsFloat",pinIsFloat);
        object.put("pinServiceKinds",type);//默认0；1：长途 ; 2:表示旅游；3表示同城
        object.put("ownerHasSeats",seats);//发布座位个数
        RequestParams params=  new RequestParams(Common.HOST+Common.HOST_PUBLISH);
        Log.d("getHead",getHeadInfo().getToken()+"getHeadInfo  "+getHeadInfo().getAppId()+"getUserId  "+getHeadInfo().getUserId());
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",getHeadInfo().getToken());
        params.addHeader("appId",getHeadInfo().getAppId());
        params.addHeader("userId",getHeadInfo().getUserId());
        params.setBodyContent(object.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                progressDialog.dismiss();
                Log.d("tag", "onSuccess: 车主发布--"+result);
                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                if (obj.getIntValue("status")==1){
                    JSONObject data = obj.getJSONObject("data");
                    Intent intent = new Intent(ReleaseActivity.this, OrderConfirmActivity.class);
                    intent.putExtra("identity",identity);
                    intent.putExtra("type",type_flag);
                    intent.putExtra("serviceId",data.getString("serviceId"));
                    startActivity(intent);

                    //SPUtil.putString(ReleaseActivity.this,"service_id",data.getString("serviceId"));
                }else if (obj.getIntValue("status")==0){
                    if (obj.getString("failure").equals("1067")){
                        failure_code= Constant.NAME_VERIFYING;
                        showCenterDialog("您的实名认证正在审核中,感谢您的耐心等待","返回","确定");
                    }else if (obj.getString("failure").equals("1068")){
                        failure_code=Constant.NAME_NOT_VERIFY;
                        showCenterDialog("为保证乘客的安全以及您的权益,车主需要实名认证后才可发布","返回","立即认证");

                    }else if (obj.getString("failure").equals("1069")){
                        failure_code=Constant.NAME_VERIFY_FALSE;
                        showCenterDialog("您的实名认证未通过,请重新提交认证材料","返回","重新提交");

                    }else if (obj.getString("failure").equals("1070")){
                        failure_code=Constant.CAR_VERIFYING;
                        showCenterDialog("您的车辆认证正在审核中,感谢您的耐心等待","返回","确定");

                    }else if (obj.getString("failure").equals("1071")){
                        failure_code=Constant.CAR_VERIFY_FALSE;
                        showCenterDialog("您的车辆认证未通过,请重新提交认证材料","返回","重新提交");

                    }else if (obj.getString("failure").equals("1066")){
                        /*failure_code=Constant.CAR_NOT_VERIFY;
                        showCenterDialog("您还没有进行车辆认证,车辆认证通过后才可抢单","返回","去认证");*/
                    }else {

                    }

                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                progressDialog.dismiss();
                Log.d("tag", "onError: 车主发布--"+ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void showCenterDialog(String text,String negative,String positive) {
        centerDialog = new CenterDialog(this, R.layout.dialog_layout, new int[]{R.id.dialog_cancel, R.id.dialog_sure});
        centerDialog.setOnCenterItemClickListener(this);
        centerDialog.show();
        TextView dialog_sure = (TextView) centerDialog.findViewById(R.id.dialog_sure);
        TextView dialog_cancel = (TextView) centerDialog.findViewById(R.id.dialog_cancel);
        TextView dialog_text = (TextView) centerDialog.findViewById(R.id.dialog_text);
        dialog_sure.setText(positive);
        dialog_cancel.setText(negative);
        dialog_text.setText(text);
    }

    private int getVolumeType(){
        if(tv_container_passenger_long_hurry.getText().toString().equals("鞋盒")){
            return 0;
        }else if (tv_container_passenger_long_hurry.getText().toString().equals("电脑主机箱")){
            return 1;
        }else {
            return 2;
        }
    }

    private void changeType(int type){
        type_flag=type;
        int flag=0;
        switch (type){
            case Constant.LONG:
                flag=0;
                break;
            case Constant.CITY:
                flag=1;
                break;
            case Constant.TRAVEL:
                flag=2;
                break;
            case Constant.LONG_HURRY:
                flag=3;
                break;

        }
        selectReleaseItem(identity,type_flag);
        for (int i = 0; i < viewList.size(); i++) {
            if (i==flag){
                viewList.get(i).setTextColor(getResources().getColor(R.color.colorAb));
                viewList.get(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.pinche_style_textview_checked));
            }else {
                viewList.get(i).setTextColor(getResources().getColor(R.color.colorbt));
                viewList.get(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.pinche_style_textview_unchecked));
            }
        }

    }

    @Override
    public void OnCenterItemClick(CenterDialog dialog, View view) {
        switch (view.getId()){
            case R.id.dialog_sure:
                switch (failure_code){
                    case Constant.NAME_VERIFYING:
                        dialog.cancel();
                        break;
                    case Constant.NAME_NOT_VERIFY:
                        startActivity(new Intent(this, MineMember.class));
                        break;
                    case Constant.NAME_VERIFY_FALSE:
                        startActivity(new Intent(this, MineMember.class));

                        break;
                    case Constant.CAR_VERIFYING:
                        dialog.cancel();

                        break;
                    case Constant.CAR_VERIFY_FALSE:
                        startActivity(new Intent(this,CarVerifyActivity.class));
                        break;
                    case Constant.CAR_NOT_VERIFY:
                        startActivity(new Intent(this,CarVerifyActivity.class));

                        break;
                    default:
                        break;
                }
                break;
            default:
                dialog.cancel();
                break;
        }
    }

    @Override
    public void onSuccess(String result,String fromWhere) {
        super.onSuccess(result,fromWhere);
        com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
        if (fromWhere.equals(Constant.PASSENGER_PUBLISH)){
            JSONObject data = obj.getJSONObject("data");
            Intent intent = new Intent(ReleaseActivity.this, OrderConfirmActivity.class);
            intent.putExtra("identity",identity);
            intent.putExtra("type",type_flag);
            intent.putExtra("serviceId",data.getString("serviceId"));
            intent.putExtra("departTime",DateUtil.getTimestamp(HandleDataUtil.withoutChinese(tv_start_time.getText().toString().trim()),"yyyy-MM-dd HH:mm"));
            startActivity(intent);
        }else if (fromWhere.equals(Constant.HOST_PUBLISH)){
            JSONObject data = obj.getJSONObject("data");
            Intent intent = new Intent(ReleaseActivity.this, OrderConfirmActivity.class);
            intent.putExtra("identity",identity);
            intent.putExtra("type",type_flag);
            intent.putExtra("serviceId",data.getString("serviceId"));
            intent.putExtra("departTime",DateUtil.getTimestamp(HandleDataUtil.withoutChinese(tv_start_time.getText().toString().trim()),"yyyy-MM-dd HH:mm"));
            startActivity(intent);
        }
    }

    @Override
    public void onSuccessError(String result, String fromWhere) {
        super.progressDialog.dismiss();
        Log.d("fromWhere",result);
        JSONObject object = JSON.parseObject(result);
        String status = object.getString("status");
        if (!status.equals("1")){
            String failure = object.getString("failure");
            ErrType.successErrCode(failure,context);

            if (failure.equals("1067")){
                failure_code= Constant.NAME_VERIFYING;
                DialogMain.carVerifyDialog("您的实名认证正在审核中,感谢您的耐心等待","返回","确定",failure_code);
            }else if (failure.equals("1068")){
                failure_code=Constant.NAME_NOT_VERIFY;
                DialogMain.carVerifyDialog("为保证乘客的安全以及您的权益,车主需要实名认证后才可发布","返回","立即认证",failure_code);

            }else if (failure.equals("1069")){
                failure_code=Constant.NAME_VERIFY_FALSE;
                //showCenterDialog("您的实名认证未通过,请重新提交认证材料","返回","重新提交");
                DialogMain.carVerifyDialog("您的实名认证未通过,请重新提交认证材料","返回","重新提交",failure_code);

            }else if (failure.equals("1070")){
                failure_code=Constant.CAR_VERIFYING;
                DialogMain.carVerifyDialog("您的车辆认证正在审核中,感谢您的耐心等待","返回","确定",failure_code);

            }else if (failure.equals("1071")){
                failure_code=Constant.CAR_VERIFY_FALSE;
                DialogMain.carVerifyDialog("您的车辆认证未通过,请重新提交认证材料","返回","重新提交",failure_code);

            }else if (failure.equals("1066")){
                failure_code=Constant.CAR_NOT_VERIFY;
                DialogMain.carVerifyDialog("您还没有进行车辆认证,车辆认证通过后才可抢单","返回","去认证",failure_code);

            }else if (failure.equals("1082")){
                showToast("订单已超时");
                DialogMain.tipDialog("订单已超时");
            }else{

            }
        }
    }

    @Override
    public void onError(Throwable ex) {
        super.onError(ex);

    }

    @Override
    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {
        //progressDialog.dismiss();
        if (i==1000){//成功
            List<DrivePath> paths = driveRouteResult.getPaths();
            DrivePath drivePath;
            if (paths.size()!=0){
                 drivePath = paths.get(0);
            }else {
                showToast("无可用路线");
                return;
            }

            float distance = drivePath.getDistance();
            if (identity==Constant.PASSENGER&&type_flag!=Constant.LONG_HURRY){
                distance_passenger_long=distance;
                seats_passenger_long=Integer.parseInt(etAmount_passenger_long.getText().toString());
                single_amount_passenger_long=new Calculator().getTotalAmountWithPassenger(type_flag+"",distance_passenger_long);
                total_amount_passenger_long = single_amount_passenger_long*seats_passenger_long;
                tv_price_passenger_long.setText(HandleDataUtil.saveToPoint2(total_amount_passenger_long) +"元");
            }else if (identity==Constant.HOST&&type_flag!=Constant.LONG_HURRY){
                distance_host_long=distance;
                seats_host_long=Integer.parseInt(etAmount_host_long.getText().toString());
                single_amount_host_long=new Calculator().getTotalAmountWithHost(type_flag+"",Constant.PRIVATE_CAR+"",distance_host_long);
                total_amount_host_long=single_amount_host_long*seats_host_long;
                loss_amount_host_long=total_amount_host_long*0.1;
                et_price_host_long.setText(HandleDataUtil.saveToPoint2(single_amount_host_long)+"");
                tv_total_price_host_long.setText(HandleDataUtil.saveToPoint2(total_amount_host_long)+"元");
                tv_break_appointment_money_host_long.setText("最高"+HandleDataUtil.saveToPoint2(loss_amount_host_long)+"元");
            }

        }else {//失败

        }
    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {

    }

    @Override
    public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

    }


}
